import React from "react";
import {ScrollView, View} from "react-native";
import chjStyle from "../../../CustomStyle";
import ModuleCommonHeader from "../../module/ModuleCommonHeader";
import CarRoadtest from "./CarRoadtest";
import Util from "../../Util";
import SwitchTextItem from "../common/SwitchTextItem";
import InputTextItem from "../common/InputTextItem";
import NextButton from "../common/NextButton";
import {Actions} from "react-native-router-flux";

export default class CarStart extends React.Component {
    // 构造函数，可执行初始化操作
    constructor(props) {
        super(props);
        // 在ES6，在构造函数里初始化state
        this.state = {};
        if (!Util.selectCar.carstart) {
            Util.selectCar.carstart = Util.defaultCarstart();
        }
    }

    // 组件被加载时
    componentDidMount() {

    }

    // 组件即将被卸载时
    componentWillUnmount() {
    }

    // 组件被渲染时
    render() {
        return (
            <View style={chjStyle.mainContainer}>
                <ModuleCommonHeader title='车辆启动检查(07)' clickedLeft={this.clickedBack.bind(this)} leftText='返回'
                                    clickedRight={this.clickedNew.bind(this)} rightText='保存'/>
                <ScrollView
                    style={chjStyle.mainList}>
                    {
                        Util.selectCar.carstart.map((item, i) => this.renderItem(item, i))
                    }
                </ScrollView>
                <NextButton onClickedListener={() => this.clickedNew()}/>
            </View>
        );
    }

    renderItem(item, i) {
        if (i === Util.selectCar.carstart.length - 1) {
            return (
                <InputTextItem object={item} autoCapitalize={'characters'} keyboardType={'default'} maxLength={19}/>);
        }
        else {
            return (<SwitchTextItem object={item} onValue='是' offValue='否' defaultState={true}/>);
        }
    }

    clickedBack() {
        Actions.pop();
    }

    clickedNew() {
        Util.saveData('cars', Util.selectCar);
        Actions.CarRoadtest(
            {
                name: 'CarRoadtest',
                component: CarRoadtest,
            }
        );
    }
}
